function add2Cart(product , count ,price){
    //检查是否已存在
    var num = $.cookie("cart[" + product + "][num]");

    //更新总价
    var total_num = $(".cart_total_num").html();
    var total_price = $(".cart_total_price").html();
    total_num = parseInt(total_num);
    total_price = parseInt(total_price);
    count = parseInt(count);

    price = parseInt(price);
    $(".cart_total_num").html(total_num * 1 + count * 1);
    $(".cart_total_price").html(total_price * 1 + price  * count);
    if (num) {//存在 +1
        $.cookie("cart[" + product + "][num]" , parseInt(num) + parseInt(count) ,{ expires: 1, path: '/' });
        $.cookie("cart[" + product + "][price]" , price ,{ expires: 1, path: '/' })
        //更新列表数量
        var oldNum = $("#cart_row_" + product).find(".cart-content-count b").html();
        var oldTotal = $("#cart_row_" + product).find("em b").html();
        oldNum = parseInt(oldNum);
        oldTotal = parseInt(oldTotal);

        $("#cart_row_" + product).find(".cart-content-count b").html(oldNum + count).trigger("create");
        $("#cart_row_" + product).find("em b").html(oldTotal + count * price).trigger("create");
    }else{//不存在 =1
        $.cookie("cart[" + product + "][num]" ,  count ,{ expires: 1, path: '/' });
        $.cookie("cart[" + product + "][price]" , price ,{ expires: 1, path: '/' });

        var id = $( "#cart_row_" + product ).find("img").attr("data");
        var url = $( "#cart_row_" + product ).find("img").attr("url");
        var img = $( "#cart_row_" + product ).find("img").attr("src");
        var name = $( "#cart_row_" + product ).find("img").attr("alt");
        $.cookie("cart[" + product + "][info][img]" , img ,{ expires: 1, path: '/' });
        $.cookie("cart[" + product + "][info][name]" , name ,{ expires: 1, path: '/' });

        var html = '<li id="cart_row_'+id+'">';
        html += '<a href="'+url+'?id='+id+'">';
        html += '<img src="'+img+'" style="width: 37px;height: 34px;"';
        html += 'alt="'+name+' width="37"height="34">';
        html += '</a>';
        html += '<span class="cart-content-count">x <b>'+count+'</b></span>';
        html += '<strong><a href="'+url+'?id='+id+'">'+name+'</a></strong>';
        html += '<em>&yen; <b>'+(count*price)+'</b></em>';
        html += '<a href="javascript:removeFromCart('+id+');" class="del-goods"><i class="fa fa-times"></i></a>';
        html += '</li>';
        $( "#cart_row_box").append($(html)).listview('refresh').trigger("create");
    }
    if (count == 0) {
        removeFromCart(product)
    }

}
function modCartNum(id,price) {
    var oldNum = $(this).attr("oldData");
    var curNum = $(this).val();
    var num = curNum - oldNum;
    add2Cart(id,num,price);
    $(this).attr("oldData",curNum);
}
function removeFromCart(product){
    //检查是否已存在

    var num = $(".cart_total_num").html();
    var price = $(".cart_total_price").html();
    num = parseInt(num);
    price = parseInt(price);
    var n = $.cookie("cart[" + product + "][num]");
    var p = $.cookie("cart[" + product + "][price]");
    $(".cart_total_num").text(num * 1 - n * 1);
    $(".cart_total_price").text(price * 1 - p * 1 * n);

    $.removeCookie( "cart[" + product + "][num]",{ path: '/' } );
    $.removeCookie( "cart[" + product + "][price]",{ path: '/' } );
    $.removeCookie( "cart[" + product + "][info][img]",{ path: '/' } );
    $.removeCookie( "cart[" + product + "][info][name]",{ path: '/' } );
    $( ".cart_row_" + product ).remove();
}
$(function () {
    $(".cart_row_num input").change(function () {
        var id = $(this).attr("id");
        var price = $(this).attr("price");
        var oldNum = $(this).attr("oldData");
        var curNum = $(this).val();
        var num = curNum - oldNum;
        if (curNum == 0 || oldNum == 0 || num == 0) {
            removeFromCart(id)
        }else{
            add2Cart(id,num,price);
            $(this).attr("oldData",curNum);
        }
    });
});